package com.stage.battle
{
	import com.greensock.TweenLite;
	import com.resource.ResAsset;
	import com.resource.cache.BitmapCacher;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	
	/**
	 * 人物头顶浮动的数字，不做程序暴击特效，直接美术资源实现
	 * 
	 */
	public class FlowText extends Sprite
	{
		public static const RED:String = "red";
		
		private var _flowHeight:int = 100;
		private var _container:Sprite = new Sprite;
		private var _isFree:Boolean;
		private var _callback:Function;
		
		public function FlowText()
		{
			super();
			mouseChildren = false;
			mouseEnabled = false;
			addChild(_container);
			_isFree = true;
		}
		
		public function showText(color:String, value:String):void{
			_isFree = false;
			var arr:Array = [];
			arr.push(color + value);
			startFlow(arr);
		}
		
		/**
		 * flag 是否现实符号
		 */
		public function show(color:String, value:int, flag:Boolean = false):void{
			_isFree = false;
			var arr:Array = [];
			if(flag && value > 0){
				arr.push(color + "a");
			}
			var str:String = value.toString();
			for(var i:int=0; i<str.length; i++){
				if(str.charAt(i) == "-"){
					arr.push(color + "d");
				}else{
					arr.push(color + str.charAt(i));
				}
			}
			startFlow(arr);
		}
		
		private function startFlow(arr:Array):void{
			var bmpArr:Array = [];
			var totalWidth:int = 0;
			for(var i:int=0; i<arr.length; i++){
				var bmp:Bitmap = new Bitmap;
				bmp.bitmapData = BitmapCacher.getInstance().getBmdByClass(ResAsset[arr[i]]);
				bmp.x = totalWidth;
				totalWidth += bmp.width;
				bmpArr.push(bmp);
				_container.addChild(bmp);
			}
			totalWidth = totalWidth / 2;
			for(var i:int=0; i<bmpArr.length; i++){
				bmpArr[i].x -= totalWidth;
			}
			
			_container.y = 0;
			TweenLite.to(_container, 2, {y:-_flowHeight, onComplete:flowComplete});
		}
		
		private function flowComplete():void{
			while(_container.numChildren > 0){
				_container.removeChildAt(0);
			}
			_isFree = true;
			// 结束的回调
			if(_callback){
				_callback.apply(null);
			}
		}
		
		public function get isFree():Boolean{
			return _isFree;
		}
		
		public function set callback(func:Function):void{
			_callback = func;
		}
	}
}